package ygflower.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

import ygflower.domain.User;
import ygflower.dao.UserDao;

public class UserController extends MultiActionController {
	public static String ErrorMessages = "errorMessages";

	private UserDao UserDao;
	private String formView;

	public void setUserDao(UserDao UserDao) {
		this.UserDao = UserDao;
	}

	private String successView;

	public String getFormView() {
		return formView;
	}

	public void setFormView(String formView) {
		this.formView = formView;
	}

	public String getSuccessView() {
		return successView;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	public ModelAndView registerUser(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String formView = "register";
		if (isFormSubmission(request)) {
			User user = (User) bindRequest(request);
			Map<String, String> errors = validatorUser(request, user);
			if (errors.size() > 0) {
				return new ModelAndView(formView).addObject(ErrorMessages,
						errors);
			} else {
				user = UserDao.save(user);

				request.getSession().setAttribute(User.CLASSNAME, user);
				return new ModelAndView(new RedirectView(getSuccessView()));
			}
		} else {
			// New form to show: render form view.
			return showNewForm(request, response);
		}
	}

	public ModelAndView loginUser(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String formView = "login";
		if (isFormSubmission(request)) {
			User user = (User) bindRequest(request);
			Map<String, String> errors = validatorLoginUser(request, user);
			if (errors.size() > 0) {
				return new ModelAndView(formView).addObject(ErrorMessages,
						errors);
			} else {

				request.getSession().setAttribute(User.CLASSNAME, user);
				return new ModelAndView(new RedirectView(getSuccessView()));
			}
		} else {
			// New form to show: render form view.
			return new ModelAndView(formView);
		}

		// UserDao.find(User.PropertyName, user.getName());

	}

	private Object bindRequest(HttpServletRequest request) {
		User user = new User();
		String name = request.getParameter(User.PropertyName);
		String password = request.getParameter(User.PropertyPassword);
		String isman = request.getParameter(User.PropertyIsman);
		String phone = request.getParameter(User.PropertyPhone);
		String address = request.getParameter(User.PropertyAddress);
		if (StringUtils.isNotBlank(name)) {
			user.setName(name);
		}
		if (StringUtils.isNotEmpty(password)) {
			user.setPassword(password);
		}
		if (StringUtils.isNotEmpty(isman)) {
			user.setIsman(User.intToBool(Integer.parseInt(isman)));
		}
		if (StringUtils.isNotBlank(phone)) {
			user.setPhone(phone);
		}
		if (StringUtils.isNotBlank(address)) {
			user.setAddress(address);
		}
		return user;
	}

	Map<String, String> validatorUser(HttpServletRequest request, Object command) {
		User user = (User) command;
		Map<String, String> errors = new HashMap<String, String>();
		if (StringUtils.isEmpty(user.getName())) {
			errors.put(User.PropertyName, "必须您的名称");
		}
		if (StringUtils.isEmpty(user.getPassword())) {
			errors.put(User.PropertyPassword, "必须输入密码");
		}
		// if (user.isIsman()) {
		// errors.put(User.PropertyIsman, "必须输入您的性别");
		// }
		return errors;
	}

	Map<String, String> validatorLoginUser(HttpServletRequest request,
			Object command) {
		User user = (User) command;
		Map<String, String> errors = new HashMap<String, String>();
		if (user == null) {
			errors.put(User.CLASSNAME, "请输入");

		} else {
			if (StringUtils.isEmpty(user.getName())) {
				errors.put(User.PropertyName, "必须您的名称");
			}
			if (StringUtils.isEmpty(user.getPassword())) {
				errors.put(User.PropertyPassword, "必须输入密码");
			}
			// if (user.isIsman()) {
			// errors.put(User.PropertyIsman, "必须输入您的性别");
			// }
		}
		if (errors.size() == 0) {
			List<User> userList = UserDao.find(User.PropertyName, user
					.getName());
			if (userList != null
					&& userList.size() > 0
					&& StringUtils.equals(user.getPassword(), userList.get(0)
							.getPassword())) {
				user = userList.get(0);
			} else {
				errors.put("loginNF", "没的找到用户");
			}
		}
		return errors;
	}

	protected boolean isFormSubmission(HttpServletRequest request) {
		return "POST".equals(request.getMethod());
	}

	protected final ModelAndView showNewForm(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		logger.debug("Displaying new form");
		return showForm(request, getFormView(), null);
	}

	protected final ModelAndView showForm(HttpServletRequest request,
			String viewName, Map controlModel) throws Exception {
		Map model = new HashMap();
		if (controlModel != null) {
			model.putAll(controlModel);
		}
		// Trigger rendering of the specified view, using the final model.
		return new ModelAndView(viewName, model);
	}
}
